package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.GoodsDao;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.model.GoodsModel;

@Repository("goodsDao")
public class GoodsDaoImpl extends BaseDaoImpl<Goods> implements GoodsDao {
	
    @Override
	public PageDataList<GoodsModel> findPageList(GoodsModel model) {
    	QueryParam param = QueryParam.getInstance();
        if(StringUtil.isNotBlank(model.getSearchName())){
        	 param.addParam("name", Operators.LIKE, model.getSearchName().trim());
        } else {
	        if (StringUtil.isNotBlank(model.getName())) {
	            param.addParam("name", Operators.LIKE, model.getName().trim());
	        }
	        if (model.getStartScore() > 0) {//积分范围查询  大于等于
	            param.addParam("score", Operators.GTE, model.getStartScore());
	        }
	        if (model.getEndScore() > 0) {//积分范围查询  小于等于
	            param.addParam("score", Operators.LTE, model.getEndScore());
	        }
	        if (model.getCategoryId() > 0) {
	        	param.addParam("goodsCategory.id", model.getCategoryId());
	        }
	        if (model.getStatus() != 99 ) {
	            param.addParam("status", model.getStatus());
	        }
	        if (model.getType() != 99 ) {
	            param.addParam("type", model.getType());
	        }
	        if (StringUtil.isNotBlank(model.getStartTime())) {
	            Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
	            param.addParam("addTime", Operators.GTE, start);
	        }
	        if (StringUtil.isNotBlank(model.getEndTime())) {
	            Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
	            param.addParam("addTime", Operators.LTE, end);
	        }
        }
        param.addOrder(OrderType.DESC, "id");
        param.addPage(model.getPage(), model.getRows());
        PageDataList<Goods> pageDataList = super.findPageList(param);
        List<GoodsModel> list = new ArrayList<GoodsModel>();
        PageDataList<GoodsModel> pageDataList_ = new PageDataList<GoodsModel>();
        for (Goods goods : pageDataList.getList()) {
            GoodsModel model_ = GoodsModel.instance(goods);
            	// GoodsPic 暂未使用，图片取Goods.picPath
                /*GoodsPic pic = goodsPicDao.getGoodsFirstPic(item.getId());
                model_.setImgSrc(pic.getPicUrl());*/
            model_.setCategoryName(goods.getGoodsCategory().getName());
            list.add(model_);
        }
        pageDataList_.setList(list);
        pageDataList_.setPage(pageDataList.getPage());
        return pageDataList_;
	}

	@Override
    public boolean updateStore(long goodsId, int store, int freezeStore, int sellAcount) {
        StringBuffer sql = new StringBuffer("UPDATE Goods SET store = store + :store,");
        sql.append(" freezeStore = freezeStore + :freezeStore, sellAcount = sellAcount + :sellAcount");
        sql.append(" WHERE id = :goodsId AND");
        sql.append(" store + :store >= 0 AND freezeStore + :freezeStore >= 0 AND sellAcount + :sellAcount >= 0  AND");
        sql.append(" type = "+GoodsModel.UP_SHELVES);
        int result = this.updateByJpql(sql.toString(), 
                new String[]{"store", "freezeStore", "sellAcount", "goodsId", "store", "freezeStore", "sellAcount"}, 
                new Object[] {store, freezeStore, sellAcount, goodsId, store, freezeStore, sellAcount});
        if (result < 1) {
            return false;
        }
        // 更新缓存
        em.refresh(this.find(goodsId));
        return true;
    }
	
}
